package com.skt.common_hr.work_sac.view.components
{
	import com.yahoo.astra.mx.controls.TimeStepper;
	import com.yahoo.astra.utils.TimeUnit;

	public class WorktimeStepper extends TimeStepper
	{
		public var interval:int = 10;
		public var beginHour:int = 9;
		public var endHour:int =6;
		
		override protected function createChildren():void
		{
			super.createChildren();
			timeInput.editable = false;
		}
		/**
		 * Decreeases the value of the selected field (and rotates back to the
		 * maximum value, if the minimum is reached). 
		 */
		override protected function decrement():void
		{
			//super.decrement();
			var useTwelveHourFormat:Boolean = this.getStyle("useTwelveHourFormat");
			var value:Date = new Date(this.value.valueOf());
			switch(this.focusedField)
			{
				case TimeUnit.MINUTES:
					var minutes:Number = value.getMinutes();
					if(minutes == 0) minutes = 60-interval;
					else minutes-=interval;
					value.setMinutes(minutes);
					break;
				case TimeUnit.SECONDS:
					var seconds:Number = value.getSeconds();
					if(seconds == 0) seconds = 59;
					else seconds--;
					value.setSeconds(seconds);
					break;
				case AMPM:
					var hours:Number = value.getHours();
					if(hours >= 12) hours -= 12;
					else hours += 12;
					value.setHours(hours);
					break;
				default: //hours
					hours = value.getHours();
					if(useTwelveHourFormat)
					{
						if(hours >= 12) //pm
						{
							if(hours == 12) hours = 23;
							else hours--;
						}
						else //am
						{
							if(hours == 0) hours = 11;
							else hours--;
						}
					}
					else
					{
						if(hours == 0) hours = 23;
						else {
							if(hours <= endHour || hours >beginHour) {
								hours--;
							}
						}
					}
					value.setHours(hours);
			}
			this.value = value;
		}
		
		/**
		 * Increases the value of the selected field (and rotates back to the
		 * minimum value, if the maximum is reached). 
		 */
		override protected function increment():void
		{
			var useTwelveHourFormat:Boolean = this.getStyle("useTwelveHourFormat");
			var value:Date = new Date(this.value.valueOf());
			switch(this.focusedField)
			{
				case TimeUnit.MINUTES:
					var minutes:Number = value.getMinutes();
					if(minutes >= (60-interval)) minutes = 0;
					else minutes+=interval;
					value.setMinutes(minutes);
					break;
				case TimeUnit.SECONDS:
					var seconds:Number = value.getSeconds();
					if(seconds == 59) seconds = 0;
					else seconds++;
					value.setSeconds(seconds);
					break;
				case AMPM:
					var hours:Number = value.getHours();
					if(hours >= 12) hours -= 12; //pm
					else hours += 12; //am
					value.setHours(hours);
					break;
				default: //hours
					hours = value.getHours();
					if(useTwelveHourFormat)
					{
						if(hours >= 12) //pm
						{
							if(hours == 23) hours = 12;
							else hours++;
						}
						else //am
						{
							if(hours == 11) hours = 0;
							else hours++;
						}
					}
					else
					{
						if(hours == 23) hours = 0;
						else {
							if(hours < beginHour || hours>=endHour) {
								hours++;
								if(hours==endHour) {
									value.setMinutes(0);
								}
							}
						}
					}
					value.setHours(hours);
			}
			this.value = value;
		}
	}
}